﻿Ext.regController('dbController', {
			
/*********************** FLIGHT TALK VIEW ***************************/
	/**
	* DESCRIPTION: get RECENT posts from people on my flight
	* PARAMETERS: 	
	* 		method - 
	* 			getFlightPost (question, review, comment, suggestion) - done
	* 			getFlightQuestion - done
	* 			getTerminalPost - done
	* 			getTerminalQuestion - done
	* 			getLangPost - done
	* 			getLangQuestion - done
	* 			getMyPost - working on it
	* 			getRecentBoredPost (suggestion) - done
	* 			getPopBoredPost - done
	* 			getRecentServicePost - recent posts on touchpoint or service
	* 			getPopServicePost  - popular posts on touchpoint or service
	*       next - next page to display
	*       list - list to display the posts
	* RESULT: populate the provided list with posts (Question, comments, suggestions, and review)
	*/
	'getMixedPosts': function (options) {
		
		// set the title for the page and the tab
		FlyTalk.views.TalkView.tweetToolbar.setTitle(options.title);
		FlyTalk.views.TalkView.items.getAt(0).tab.setText(options.tab1);
		FlyTalk.views.TalkView.items.getAt(1).tab.setText(options.tab2);
		FlyTalk.views.TalkView.items.getAt(3).tab.hide();
		FlyTalk.views.TalkView.items.getAt(3).hide();
		var dbname = FlyTalk.views.BoredView.opened.title;

		Ext.util.JSONP.request({
				url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
				callbackKey: 'callback',
				params: {
               			// method: 'getFlightPost',	 // using this parameter
						 method: options.method1,	
						 db: dbname,
            	},
				callback: function(data){
					//alert(data.results[0].from_user);
					//alert(data.results.length);
					//alert(data.results[0].text);
					// load data into store
				    FlyTalk.views.ColumnView.MixedStore.loadData(data.results, false);
				    FlyTalk.views.ColumnView.tweetList.refresh();
				}
		});
		
		FlyTalk.views.TalkView.items.getAt(0).show();
		
		if(options.title == 'myFeed'){//TODO - change my talk
			FlyTalk.views.TalkView.items.getAt(1).tab.hide();
			FlyTalk.views.TalkView.items.getAt(2).tab.hide();
			FlyTalk.views.TalkView.items.getAt(3).tab.show();
			FlyTalk.views.TalkView.items.getAt(3).tab.setText(options.tab2);
			//FlyTalk.views.TalkView.items.getAt(3).show();
		}else{
			FlyTalk.views.TalkView.items.getAt(1).tab.show();
			FlyTalk.views.TalkView.items.getAt(2).tab.show();
			Ext.util.JSONP.request({
				url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
				callbackKey: 'callback',
				params: {
	           			// method: 'getFlightPost',	 // using this parameter
						 method: options.method2,	
						 db: dbname,
	        	},
				success: function() { Ext.Msg.alert('Success'); },
				failure: function() { Ext.Msg.alert('fail');},
				callback: function(data){
					FlyTalk.views.ColumnView1.MixedStore.loadData(data.results, false);
					 FlyTalk.views.ColumnView1.tweetList.refresh();
					
				}
			});
			
			
			// check if there is a third tab
			if(options.numtabs == 3){
				FlyTalk.views.TalkView.items.getAt(2).tab.setText(options.tab3);
				Ext.util.JSONP.request({
					url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
					callbackKey: 'callback',
					params: {
		           			// method: 'getFlightPost',	 // using this parameter
							 method: options.method3,
							 db: dbname,
		        	},
					success: function() { Ext.Msg.alert('Success'); },
					failure: function() { Ext.Msg.alert('fail');},
					callback: function(data){
						FlyTalk.views.ColumnView2.MixedStore.loadData(data.results, false);
						 FlyTalk.views.ColumnView2.tweetList.refresh();
					}
				});
			}else{
				FlyTalk.views.TalkView.items.getAt(2).tab.hide();
			}
		}
		
		//FlyTalk.views.TalkView.setCard(FlyTalk.views.ColumnView1);
		if(options.prev == 'home'){
			FlyTalk.views.mainView.setActiveItem(FlyTalk.views.TalkView, {type: 'slide', direction: 'left'});
		}else{
			FlyTalk.views.mainView.setActiveItem(FlyTalk.views.TalkView, {type: 'slide', direction: 'right'});
		}
		
	}, // end of getMixedPost

	/**
	* DESCRIPTION: get a list of tasks that the user can help out with
	* PARAMETERS: 
	*       next - next page to display
	*       list - list to display the search results
	* RESULT: populate the provided list with help out and direct to next page
	*/
	'getHelpList': function (options) {
		alert("in get help list");	
	}, // end of getHelpList
	
	/**
	* DESCRIPTION: get user's notifications
	* PARAMETERS: 
	*       next - next page to display
	*       list - list to display the search results
	* RESULT: 
	* 		populate the provided list with notifications and direct to next page
	* 		notification will start with official, then 2 help out + 1 see more and recent notifications
	*/
	'getNotification': function (options) {
		var dbname = FlyTalk.views.BoredView.opened.title;
		
		Ext.util.JSONP.request({
			url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
			callbackKey: 'callback',
			params: {
           			// method: 'getFlightPost',	 // using this parameter
					 method: 'getNotification',	
					 db: dbname,
        	},
			callback: function(data){
				// load data into store
			    FlyTalk.views.NotificationView.noteStore.loadData(data.results, false);
			    FlyTalk.views.NotificationView.list.refresh();
			}
		});
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.NotificationView, {type: 'slide', direction: 'up'});
	}, // end of getNotification
	
	/**
	* DESCRIPTION: get user's notifications
	* PARAMETERS: 
	*       next - next page to display
	*       list - list to display the search results
	* RESULT: 
	* 		populate the provided list with notifications and direct to next page
	* 		notification will start with official, then 2 help out + 1 see more and recent notifications
	*/
	'getNewNote': function (options) {
		var dbname = FlyTalk.views.BoredView.opened.title;
		
		Ext.util.JSONP.request({
			url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
			callbackKey: 'callback',
			params: {
           	    // method: 'getFlightPost',	 // using this parameter
			    method: 'getNewNote',	
			    db: dbname,
        	},
			callback: function(data){
				FlyTalk.views.HomeView.noteButton.setBadge(data.results.length);			   
			}
		});
		//return 2;
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.HomeView, {type: 'slide', direction: 'right'});
	}, // end of getNotification
	
	/**
	* DESCRIPTION: save user's question 
	* PARAMETERS: 
	*       next - next page to display
	*       text - comment
	* RESULT: 
	* 		will update the database and direct user to next page
	*/
	'saveQuestion': function (options) {
		var my = 0;
		var dbname = FlyTalk.views.BoredView.opened.title;
		
		if(options.uid == 3){
			my = 1;
		}
		Ext.util.JSONP.request({
			url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
			callbackKey: 'callback',
			params: {
					 method: 'saveQuestion',
					 uid: options.uid,
				     text: options.text,
				     mine: my,
				     db: dbname,
        	},
			success: function() { Ext.Msg.alert('Success'); },
			failure: function() { Ext.Msg.alert('fail');},
			callback: function(data){
				//FlyTalk.views.ColumnView2.MixedStore.loadData(data.results);
			}
		});
		
		var next = FlyTalk.views.TalkView.tweetToolbar.title; // see which page talk is on right now
		FlyTalk.views.BoredView.status.show();
		Ext.dispatch({
            controller: FlyTalk.controllers.ftController,
            action: 'openNext',
            next: next,
        });
	}, // end of saveQuestion
	
	/**
	* DESCRIPTION: save user's comment (not a reply)
	* PARAMETERS: 
	*       next - next page to display
	*       text - comment
	* RESULT: 
	* 		will update the database and direct user to next page
	*/
	'saveComment': function (options) {
		var my = 0;
		var dbname = FlyTalk.views.BoredView.opened.title;
		
		if(options.uid == 3){
			my = 1;
		}
		Ext.util.JSONP.request({
			url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
			callbackKey: 'callback',
			params: {
					 method: 'saveComment',
					 uid: options.uid,
				     text: options.text,
				     mine: my,
				     db: dbname,
        	},
			success: function() { Ext.Msg.alert('Success'); },
			failure: function() { Ext.Msg.alert('fail');},
			callback: function(data){
				//FlyTalk.views.ColumnView2.MixedStore.loadData(data.results);
			}
		});
		
		var next = FlyTalk.views.TalkView.tweetToolbar.title; // see which page talk is on right now
		FlyTalk.views.BoredView.status.show();
		Ext.dispatch({
            controller: FlyTalk.controllers.ftController,
            action: 'openNext',
            next: next,
        });
	},  // end of saveForm
	
	/**
	* DESCRIPTION: save user's reply 
	* PARAMETERS: 
	*       next - next page to display
	*       text - comment
	*       thread id - id of the thread
	* RESULT: 
	* 		will update the database and direct user to next page
	*/
	'saveReply': function (options) {
		//alert("db save question");
	//	alert(options.uid);
		var dbname = FlyTalk.views.BoredView.opened.title;
		var thread = FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data.thread;
		var from_uid = FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data.from_uid; // original user of the post
		var my = 0;
		if(options.uid == 3){
			my = 1;
		}
		Ext.util.JSONP.request({
			url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
			callbackKey: 'callback',
			params: {
					 method: 'saveReply',
					 uid: options.uid,
				     text: options.text,
				     tid: from_uid,
				     mine: my,
				     thread: thread,
				 	db: dbname,
        	},
			success: function() { Ext.Msg.alert('Success'); },
			failure: function() { Ext.Msg.alert('fail');},
			callback: function(data){
				//FlyTalk.views.ColumnView2.MixedStore.loadData(data.results);
			}
		});
		
		var next = FlyTalk.views.TalkView.tweetToolbar.title; // see which page talk is on right now
		FlyTalk.views.BoredView.status.show();
		Ext.dispatch({
            controller: FlyTalk.controllers.ftController,
            action: 'openNext',
            next: next,
        });
	}, // end of saveForm
	

	'saveSuggestion': function (options) {
		var my = 0;
		var dbname = FlyTalk.views.BoredView.opened.title;
		if(options.uid == 3){
			my = 1;
		}
		//alert("db: "+options.uid);
		
		
		Ext.util.JSONP.request({
			url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
			callbackKey: 'callback',
			params: {
					 method: 'saveSuggestion',
					 uid: options.uid,
			         text: options.comment,
			         mine: my,
			         db: dbname,
        	},
			callback: function(data){
				//FlyTalk.views.ColumnView2.MixedStore.loadData(data.results);
			}
		});
		
		// next page
		var next = FlyTalk.views.TalkView.tweetToolbar.title; // see which page talk is on right now
		FlyTalk.views.BoredView.status.show();
		Ext.dispatch({
            controller: FlyTalk.controllers.ftController,
            action: 'openNext',
            next: next,
        });
	}, // end of saveForm
	
	/**
	* DESCRIPTION: save user's review 
	* PARAMETERS: 
	*       next - next page to display
	*       text - comment
	*       recRest - recommend restaurant?
	*       food - what did you eat?
	*       price - how much was the food?
	*       wait - how long was the wait
	*       recFood - recommend food?
	* RESULT: 
	* 		will update the database and direct user to next page
	*/
	'saveReview': function (options) {
		var dbname = FlyTalk.views.BoredView.opened.title;
		// alert("save question");
		//alert("rec food: "+ options.recFood + " recRest: "+ options.recRest + " eat: "+ options.eat+ " cost: "+ options.cost+ " wait: "+ options.wait+ " comment: "+ options.comment);
		var my = 0;
		if(options.uid == 3){
			my = 1;
		}
		
		Ext.util.JSONP.request({
			url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
			callbackKey: 'callback',
			params: {
					 method: 'saveReview',
					 uid: options.uid,
			         recRest: options.recRest,
			         recFood: options.recFood,
			         food: options.eat,
			         price: options.cost,
			         wait: options.wait,
			         text: options.comment,
			         mine: my,
			         tp: options.tp,
			         db: dbname,
        	},
			callback: function(data){
				//FlyTalk.views.ColumnView2.MixedStore.loadData(data.results);
			}
		}); 
		
		// link to next page
		var next = FlyTalk.views.TalkView.tweetToolbar.title; // see which page talk is on right now
		FlyTalk.views.BoredView.status.show();
		Ext.dispatch({
            controller: FlyTalk.controllers.ftController,
            action: 'openNext',
            next: next,
        });
	}, // end of saveForm
	
	/**
	* DESCRIPTION: search database according to the input submitted
	* PARAMETERS: 
	* 		input - user input
	*       next - next page to display
	*       list - list to display the search results
	* RESULT: 
	* 		populate the provided list with search results and direct to next page
	* 		if there are official content in search result, will place those at the top
	*/
	'searchDB': function (options) {
		alert("in search db");
	}, // end of search db
	
	/**
	* DESCRIPTION: save the thumb up to the database
	* PARAMETERS: 
	* 		fid - id of the form
	*       next - next page to display
	* RESULT: 
	* 		will update the database and direct user to next page
	*/
	'up': function (options) {
		var dbname = FlyTalk.views.BoredView.opened.title;
		var id = FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data.fid;
		FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data.rated = 1;
		Ext.util.JSONP.request({
			url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
			callbackKey: 'callback',
			params: {
				 method: 'up',
				// fid: FlyTalk.views.ColumnView.MixedStore.getAt(options.fid).data,fid,
				 fid: id,
				 db: dbname,
			},
			callback: function(data){
			   // alert(data.results[0].username);
				//var tweetList = data.results;
				//store.loadData(tweetList);
				//list.bindStore(store); 	
			}
		});
	}, // end of up
	
	'down': function (options) {
		var dbname = FlyTalk.views.BoredView.opened.title;
		var id = FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data.fid;
		FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data.rated = 1;
		Ext.util.JSONP.request({
			url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
			callbackKey: 'callback',
			params: {
				 method: 'down',
				// fid: FlyTalk.views.ColumnView.MixedStore.getAt(options.fid).data,fid,
				 fid: id,
				 db: dbname,
			},
			callback: function(data){
			   // alert(data.results[0].username);
				//var tweetList = data.results;
				//store.loadData(tweetList);
				//list.bindStore(store); 
				
			}
		});
	}, // end of down
	
	'follow': function (options) {
		var dbname = FlyTalk.views.BoredView.opened.title;
		var id = FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data.fid;
		FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data.followed = 1;
		Ext.util.JSONP.request({
			url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
			callbackKey: 'callback',
			params: {
				 method: 'follow',
				// fid: FlyTalk.views.ColumnView.MixedStore.getAt(options.fid).data,fid,
				 fid: id,
				 db: dbname,
			},
			callback: function(data){
			   // alert(data.results[0].username);
				//var tweetList = data.results;
				//store.loadData(tweetList);
				//list.bindStore(store); 
				
			}
		});
	}, // end of follow
	
	'unfollow': function (options) {
		var dbname = FlyTalk.views.BoredView.opened.title;
		var id = FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data.fid;
		FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data.followed = 0;
		Ext.util.JSONP.request({
			url: 'http://yangjustine.startlogic.com/flytalk/flytalk1/db/dbFlytalk.php',
			callbackKey: 'callback',
			params: {
				 method: 'unfollow',
				// fid: FlyTalk.views.ColumnView.MixedStore.getAt(options.fid).data,fid,
				 fid: id,
				 db: dbname,
			},
			callback: function(data){
			   // alert(data.results[0].username);
				//var tweetList = data.results;
				//store.loadData(tweetList);
				//list.bindStore(store); 
			}
		});
	}, // end of unfollow
	
	'openThread': function (options) {
		//alert(options.index);
		var tid = FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data.thread;
		//alert("db open thread. tid: "+tid);
		
	}, // end of open thread
	
	'getRating': function (options) {
		alert("get rating");
		// get what user said about the comment (thumbs up or down)
	}, // end of has rated
	
	'getFollowStatus': function (options) {
		alert("get follow");
		// get follow status
	}, // end of get follow status
	
	'getRestRating': function (options) {
		alert("get restaurant rating");
	}, // end of get restaurant rating
	

}); // end of controller

FlyTalk.controllers.dbController = Ext.ControllerManager.get('dbController');